#include "ui.h"
#include "lcd.h"

#define OFFSET_X(x) (36 + (x)) // X坐标偏移
#define OFFSET_Y(y) (80 + (y)) // Y坐标偏移

#define HISTOGRAM_HIGH 120 // 柱状图最高值

// ui背景框图
void UI_Background()
{
    uint8_t i, j;

    //标题显示 黑色显示
    g_back_color = WHITE;

    // LCD_Fill(OFFSET_X(-26, OFFSET_Y(-72, OFFSET_X(-6, OFFSET_Y(-5, BLACK);	// 左上角矩形，现用作工业屏接入指示
    lcd_show_string(OFFSET_X(36), OFFSET_Y(-70), 24 * 3, 24, 24, "HX6203", BLACK);
    lcd_fill(OFFSET_X(4), OFFSET_Y(-40), OFFSET_X(136), OFFSET_Y(-38), BLACK);
    lcd_show_string(OFFSET_X(4), OFFSET_Y(-32), 24 * 6, 24, 24, __DATE__, BLACK);

    //柱状图表格绘制，红色显示
    for (i = 0; i < 17; i++)
    {
        //表格竖线 10条 正反功率
        if (i < 10)
        {
            lcd_draw_line(OFFSET_X(i * 81), OFFSET_Y(0), OFFSET_X(i * 81), OFFSET_Y(320), RED);
            if (i < 9)
            {
                lcd_show_string(OFFSET_X(5 + i * 81), OFFSET_Y(283), 16 * 5, 16, 16, "P-:     W", RED);
                lcd_show_string(OFFSET_X(5 + i * 81), OFFSET_Y(300), 16 * 5, 16, 16, "P+:     W", RED);
            }
        }
        //表格横线  5条
        if (i == 0 || i == 2 || i == 8 || i == 14 || i == 16)
            lcd_draw_line(OFFSET_X(0), OFFSET_Y(i * 20), OFFSET_X(728), OFFSET_Y(i * 20), RED);
        if (i > 2 && i < 14 && i != 8)
        {
            for (j = 0; j < 28; j++)
            { //表格刻度线
                lcd_draw_line(OFFSET_X(-2 + j * 27), OFFSET_Y(i * 20), OFFSET_X(2 + j * 27), OFFSET_Y(i * 20), RED);
            }
        }
    }

    //电流 温度 符号/单位，红色显示
    lcd_show_string(OFFSET_X(-26), OFFSET_Y(12), 16 * 3, 16, 16, "I/A", RED);
    lcd_show_string(OFFSET_X(732), OFFSET_Y(12), 16 * 3, 16, 16, "I/A", RED);

    lcd_show_string(OFFSET_X(-33), OFFSET_Y(290), 16 * 3, 16, 16, "T/^C", RED);
    lcd_show_string(OFFSET_X(732), OFFSET_Y(290), 16 * 3, 16, 16, "T/^C", RED);

    //电流 温度两端刻度值，黑色显示
    for (i = 0; i < 5; i++) //电流刻度值
    {
        lcd_show_num(OFFSET_X(-20), OFFSET_Y(52 + i * 20), 10 - i * 2, 2, 16, BLACK);
        lcd_show_num(OFFSET_X(732), OFFSET_Y(52 + i * 20), 10 - i * 2, 2, 16, BLACK);
    }
    for (i = 0; i < 5; i++) //温度刻度值
    {
        lcd_show_num(OFFSET_X(-30), OFFSET_Y(172 + i * 20), 100 - i * 20, 3, 16, BLACK);
        lcd_show_num(OFFSET_X(732), OFFSET_Y(172 + i * 20), 100 - i * 20, 3, 16, BLACK);
    }

    //种子源及输出状态矩形框100*60，黑色显示
    //两路DA状态矩形框
    lcd_draw_rectangle(OFFSET_X(194), OFFSET_Y(-70), OFFSET_X(294), OFFSET_Y(-10), BLACK);
    // lcd_show_string(OFFSET_X(265), OFFSET_Y(-57), 24 * 1, 24, 24, ".", BLACK);
    // lcd_show_string(OFFSET_X(265), OFFSET_Y(-36), 24 * 1, 24, 24, "*C", BLACK);
    // lcd_show_string(OFFSET_X(265), OFFSET_Y(-67), 24 * 1, 24, 24, "A", BLACK);

    lcd_draw_rectangle(OFFSET_X(434), OFFSET_Y(-70), OFFSET_X(534), OFFSET_Y(-10), BLACK);

    // lcd_show_string(OFFSET_X(505), OFFSET_Y(-57), 24 * 1, 24, 24, ".", BLACK);
    // lcd_show_string(OFFSET_X(473), OFFSET_Y(-36), 24 * 1, 24, 24, "*C", BLACK);
    // lcd_show_string(OFFSET_X(473), OFFSET_Y(-67), 24 * 1, 24, 24, "A", BLACK);

    //合路矩形框
    lcd_draw_rectangle(OFFSET_X(294), OFFSET_Y(-55), OFFSET_X(434), OFFSET_Y(-25), BLACK);
    //向下输出箭头
    //    lcd_draw_line(OFFSET_X(+364, OFFSET_Y(-2, OFFSET_X(+341, OFFSET_Y(-25);
    //    lcd_draw_line(OFFSET_X(+364, OFFSET_Y(-2, OFFSET_X(+387, OFFSET_Y(-25);

    //种子源矩形框
    lcd_draw_rectangle(OFFSET_X(567), OFFSET_Y(-70), OFFSET_X(729), OFFSET_Y(-10), BLACK);
    lcd_draw_line(OFFSET_X(567), OFFSET_Y(-50), OFFSET_X(729), OFFSET_Y(-50), BLACK);
    lcd_draw_line(OFFSET_X(648), OFFSET_Y(-50), OFFSET_X(648), OFFSET_Y(-10), BLACK);

    lcd_show_string(OFFSET_X(605), OFFSET_Y(-69), 16 * 6, 16, 16, "SEED SOURCE", BLACK);
    lcd_show_string(OFFSET_X(634), OFFSET_Y(-43), 12 * 1, 24, 24, "V", BLACK);
    lcd_show_string(OFFSET_X(715), OFFSET_Y(-43), 12 * 1, 24, 24, "A", BLACK);

    //种子源箭头
    lcd_draw_line(OFFSET_X(537), OFFSET_Y(-40), OFFSET_X(567), OFFSET_Y(-10), BLACK);
    lcd_draw_line(OFFSET_X(537), OFFSET_Y(-40), OFFSET_X(567), OFFSET_Y(-70), BLACK);
    //*暂时在ui刷新中调用
    for (i = 0; i < 9; i++)
    {
        //九路输出箭头
        lcd_draw_line(OFFSET_X(40 + i * 81), OFFSET_Y(344), OFFSET_X(17 + i * 81), OFFSET_Y(321), BLACK);
        lcd_draw_line(OFFSET_X(40 + i * 81), OFFSET_Y(344), OFFSET_X(63 + i * 81), OFFSET_Y(321), BLACK);
        //刀片序号
        lcd_show_num(OFFSET_X(37 + i * 81), OFFSET_Y(321), i + 1, 1, 16, BLACK);
    }

    //合路横线
    lcd_draw_line(OFFSET_X(39), OFFSET_Y(344), OFFSET_X(689), OFFSET_Y(344), BLACK);

    //总功率输出框
    lcd_draw_line(OFFSET_X(304), OFFSET_Y(345), OFFSET_X(304), OFFSET_Y(399), BLACK);
    lcd_draw_line(OFFSET_X(425), OFFSET_Y(345), OFFSET_X(425), OFFSET_Y(399), BLACK);
    lcd_show_string(OFFSET_X(342), OFFSET_Y(351), 16 * 3, 16, 16, "OUTPUT", BLACK);
    lcd_show_string(OFFSET_X(400), OFFSET_Y(375), 16 * 1, 16, 16, "W", BLACK);

    //设定功率与反向功率状态框 121*45
    lcd_draw_rectangle(OFFSET_X(162), OFFSET_Y(350), OFFSET_X(283), OFFSET_Y(395), BLACK);
    lcd_show_string(OFFSET_X(171), OFFSET_Y(351), 16 * 7, 16, 16, "REVERSE POWER", BLACK);
    lcd_show_string(OFFSET_X(258), OFFSET_Y(375), 16 * 1, 16, 16, "W", BLACK);
    lcd_draw_rectangle(OFFSET_X(445), OFFSET_Y(350), OFFSET_X(566), OFFSET_Y(395), BLACK);
    lcd_show_string(OFFSET_X(470), OFFSET_Y(351), 16 * 5, 16, 16, "SET POWER", BLACK);
    lcd_show_string(OFFSET_X(540), OFFSET_Y(375), 16 * 1, 16, 16, "W", BLACK);

    //错误计数及错误代码 红色背景 白色显示，信息内容为白色背景 红色显示
    g_back_color = RED;
    //(sx,sy),(ex,ey):填充矩形对角坐标,区域大小为:(ex-sx+1)*(ey-sy+1)
    lcd_fill(OFFSET_X(-29), OFFSET_Y(358), OFFSET_X(52), OFFSET_Y(390), RED);
    lcd_show_string(OFFSET_X(-27), OFFSET_Y(358), 16 * 5, 16, 16, "Err code:", WHITE);

    //    lcd_show_string(OFFSET_X(-27, OFFSET_Y(+374, 16*5, 16, 16, "ERR code :");

    //    BACK_COLOR = WHITE;
    //    POINT_COLOR = RED;
    //    char* str = HexToString(0);
    //    lcd_show_string(OFFSET_X(+55, OFFSET_Y(+374, 16*1, 16, 16, "0x");
    //    lcd_show_string(OFFSET_X(+55, OFFSET_Y(+374, 16*2, 16, 16, (u8*)str);
    //    free(str);

    //运行计时 蓝色背景 白色显示
    g_back_color = BLUE;
    //    POINT_COLOR = WHITE;
    lcd_fill(OFFSET_X(674), OFFSET_Y(358), OFFSET_X(756), OFFSET_Y(390), BLUE);
    lcd_show_string(OFFSET_X(676), OFFSET_Y(358), 16 * 5, 16, 16, "Run time :", WHITE);
}